using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Transfer_Ctrl : MonoBehaviour
{

    public Image fadeImage;
    public float fadeDuration = 1.0f;

    public void Init()
    {
        fadeImage.color = Color.clear;
    }

    public void StartSceneTransition(string nextScene, ref bool useTransfer){
        fadeImage.color = new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, 0);
        StartCoroutine(Transition(nextScene));
        useTransfer =  true;
    }

    private IEnumerator Transition(string nextScene){
        yield return StartCoroutine(Fade(1));

        SceneManager.LoadScene(nextScene);

    }


    public IEnumerator Fade(float targetAlpha){
        float startAlpha = fadeImage.color.a;
        float timer = 0;

        while(timer < fadeDuration){
            timer += Time.deltaTime;
            float alpha = Mathf.Lerp(startAlpha, targetAlpha, timer/fadeDuration);

            fadeImage.color = new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, alpha);
            yield return null;
        }

        fadeImage.color = new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, targetAlpha);
    }
}
